/*
 * Copyright (c) 2020. Bailiang All rights reserved. 广州百量科技有限公司 版权所有
 * 请勿修改或删除版权声明及文件头部.
 */

package top.hundreds.oasys.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class MvcHttpUtil {

    public static Map<String, String> getRequestParameters() {
        HashMap<String, String> values = new HashMap<>(20);
        HttpServletRequest request = MvcHttpUtil.getRequest();
        Enumeration<String> enums = request.getParameterNames();
        while (enums.hasMoreElements()) {
            String paramName = enums.nextElement();
            String paramValue = request.getParameter(paramName);
            values.put(paramName, paramValue);
        }
        return values;
    }

    public static HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
    }

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    public static Long getCurrAdminId() {
        String id = (String) getRequest().getAttribute("id");
        return Long.valueOf(id);
    }

    public static String getCurrAdminName() {
        return (String) getRequest().getAttribute("name");
    }

    public static Long getCurrUserId() {
        String id = (String) getRequest().getAttribute("id");
        return Long.valueOf(id);
    }


}
